//
//  ScoreTeacherShortCutViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-14.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ScoreTeacherShortCutViewController.h"
#import "ScoreTeacherSelectViewController.h"
#import "ScoreTableViewCell.h"
#import "ChildInfo.h"
#import "UIImageView+AFNetworking.h"
#import "NetworkAccessManager.h"

@interface ScoreTeacherShortCutViewController()<ScoreTeacherSelectViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet RateView *rateView0;
@property (weak, nonatomic) IBOutlet RateView *rateView1;
@property (weak, nonatomic) IBOutlet RateView *rateView2;
@property (weak, nonatomic) IBOutlet RateView *rateView3;


@property (strong, nonatomic)NSMutableArray *selectedIndexPathArray;

@end

@implementation ScoreTeacherShortCutViewController{
    
    UITapGestureRecognizer  *_backgroundTap;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.rateView0.delegate = self;
    [self.rateView0 setMaxRating:3];
    [self.rateView0 setRating:2];
    self.rateView1.delegate = self;
    [self.rateView1 setMaxRating:3];
    [self.rateView1 setRating:2];
    self.rateView2.delegate = self;
    [self.rateView2 setMaxRating:3];
    [self.rateView2 setRating:2];
    self.rateView3.delegate = self;
    [self.rateView3 setMaxRating:3];
    [self.rateView3 setRating:2];
    
    _option0 = 2;
    _option1 = 2;
    _option2 = 2;
    _option3 = 2;
    
//    [self queryClassChild];
    self.selectedIndexPathArray = [NSMutableArray array ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"toSelectChildView"]) {
        UINavigationController *navViewController = segue.destinationViewController;
        ScoreTeacherSelectViewController *destViewController = navViewController.viewControllers[0];
        
//        destViewController.selectedIndexPathArray = self.lParentViewController.selectedIndexPathArray;
        destViewController.selectedChildArray = self.selectedChildArray;
        destViewController.delegate = self;
        destViewController.unSelectedChildArray = self.unSelectedChildArray;
        destViewController.selectedIndexPathArray = self.selectedIndexPathArray;
        
        destViewController.collectionData = [ChildInfo sortByGenderAndNameFromChildListFromeRemoteData:self.allChildArray];
    }
}



#pragma mark - RateViewDelegate
- (void)rateView:(RateView *)rateView ratingDidChange:(float)rating{
    NSLog(@"ratingDidChange");
    switch (rateView.tag) {
        case 100:
            _option0 = rating;
            break;
        case 101:
            _option1 = rating;
            break;
        case 102:
            _option2 = rating;
            break;
        case 103:
            _option3 = rating;
            break;
        default:
            break;
    }
}

#pragma mark - ScoreTeacherSelectViewControllerDelegate
- (void)scoreTeacherSelectViewControlleWasDone:(NSMutableArray *)selectedIndexPathArray childArray:(NSMutableArray *) childArray unSelectedChildArray:(NSMutableArray *) unSelectedChildArray{
    NSLog(@"method scoreTeacherSelectViewControlleWasDone ");
    
//    self.selectedIndexPathArray = selectedIndexPathArray;
    [self.tableView reloadData];

}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.selectedChildArray) {
        return [self.selectedChildArray count]+1;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    static NSString *cellAddId = @"CellAdd";
    
    
    if (self.selectedChildArray) {
        if (indexPath.item == [self.selectedChildArray count]) {
            UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:cellAddId];
            return addCell;
        }else{
            ScoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            
            ChildInfo *child = self.selectedChildArray[indexPath.item];
            [cell.name setText:child.name];
            [cell.photo setImageWithURL:child.photoUrl placeholderImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
            
            cell.childData = child;
            
            return cell;
        }
    }else{
        UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:cellAddId];
        return addCell;
    }
    
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedChildArray) {
        if (indexPath.row < [self.selectedChildArray count]) {
            return NO;
        }else{
            return YES;
        }
    }
    return YES;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"selected item at %@", indexPath);
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self performSegueWithIdentifier:@"toSelectChildView" sender:nil];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedChildArray) {
        if (indexPath.row < [self.selectedChildArray count]) {
            return 115;
        }else{
            return 44;
        }
    }else{
        return 44;
    }
    
}


#pragma mark - control keyboard
- (void)touchBackgroundAction:(UITapGestureRecognizer *)sender {
    NSLog(@"touchBackgroundAction");
    [self.view endEditing:YES];
}

- (void)keyboardWasShown:(NSNotification *)notification {
    NSLog(@"keyboardWasShown");
    

    //add bg event
    _backgroundTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchBackgroundAction:)];
    [self.view addGestureRecognizer:_backgroundTap];
}


- (void)keyboardWillHide:(NSNotification *)notification {
    NSLog(@"keyboardWillHide");
    

    if (_backgroundTap) {
        [self.view removeGestureRecognizer:_backgroundTap];
    }
}


-(NSMutableArray *)allChildArray{
    if (!_allChildArray) {
        _allChildArray = [[NSMutableArray alloc]init];
    }
    return _allChildArray;
}
@end
